﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SAPbobsCOM;
using System.Reflection;
using System.Runtime.Serialization;

namespace CompanySyncProject
{
    public class DIOrderService : BaseRun
    {
        public DIOrderService(Company co, System.IO.StreamWriter lo)
            : base(co, lo)
        {

        }

        public override void Run()
        {
            oCompany.XmlExportType = BoXmlExportTypes.xet_ExportImportMode;
            oCompany.XMLAsString = true;
            /*
            DepartmentsService oDeptSrv;
            oDeptSrv = (DepartmentsService)(oCompany.GetCompanyService().GetBusinessService(ServiceTypes.DepartmentsService));

            SAPbobsCOM.Department addLine;
            addLine = (SAPbobsCOM.Department)oDeptSrv.GetDataInterface(DepartmentsServiceDataInterfaces.dsDepartment);

            addLine.Name = "B1";
            addLine.Description = "Business One";
            oDeptSrv.AddDepartment(addLine); 
            */

            /*SAPbobsCOM.Documents order = oCompany.GetBusinessObject(BoObjectTypes.oPurchaseOrders);
            order.CardCode = "abc";
            order.DocDueDate = DateTime.Now;
            order.Lines.ItemCode = "sss";
            order.Lines.Quantity = 32;
            order.SaveXML(@"..\..\test\output.xml");
            */
            
            //add part
            
            //SAPbobsCOM.Documents order = oCompany.GetBusinessObjectFromXML(@"..\..\test\add1.txt", 0);
            //string cardcode = order.CardCode;
            //string cardname = order.CardName;
            //order.Lines.SetCurrentLine(0);
            //string itemcode = order.Lines.ItemCode;
            //double qty = order.Lines.Quantity;

            //order.Add();
            
            /*
            //update part need to have all
            
            SAPbobsCOM.Documents order = oCompany.GetBusinessObject(BoObjectTypes.oPurchaseOrders);
            order.GetByKey(6);
            order.Browser.ReadXml(@"..\..\test\import3.txt", 0);
            order.Update();
            

            /*
            SAPbobsCOM.Documents order = oCompany.GetBusinessObject(BoObjectTypes.oPurchaseOrders);
            order.GetByKey(4);
            order.UpdateFromXML(@"..\..\test\import3.txt");
            */

            //savexml
            /*
            SAPbobsCOM.Documents order = oCompany.GetBusinessObject(BoObjectTypes.oPurchaseOrders);
            order.GetByKey(6);
            order.SaveXML(@"..\..\test\output2.xml");
            */


            //SAPbobsCOM.Documents order = oCompany.GetBusinessObject(BoObjectTypes.oPurchaseOrders);
            //order.GetByKey(6);
            //order.UpdateFromXML(@"..\..\test\add1.txt");
            //order.Browser.ReadXml(@"..\..\test\add1.txt", 0);
            //string code = order.Lines.ItemCode;
            //order.Lines.SetCurrentLine(1);
            //code = order.Lines.ItemCode;
            //order.Update();
            //order.Lines.BaseType = 540000006;
            //order.Lines.BaseEntry = 1;
            //order.Lines.BaseLine = 0;
            //order.Lines.Quantity = 20;
            //order.Update();

            //SAPbobsCOM.Documents draft = oCompany.GetBusinessObject(BoObjectTypes.oDrafts);
            //draft.DocObjectCode = BoObjectTypes.oInvoices;
            //draft.CardCode = "ACQUIRE";
            //draft.DocDate = DateTime.Now;
            //draft.Add();
            //draft.GetByKey (1);
            //draft.SaveDraftToDocument();
            //draft.SaveXML(@"..\..\test\draft.xml");

            oCompany.XmlExportType = BoXmlExportTypes.xet_ExportImportMode;
            oCompany.XMLAsString = true;

            //SAPbobsCOM.Documents draft = oCompany.GetBusinessObject(BoObjectTypes.oDrafts);
            //draft.GetByKey(34);
            //draft.SaveXML(@"..\..\test\testexportdraft.txt");

            //SAPbobsCOM.Documents obj21 = oCompany.GetBusinessObject(BoObjectTypes.oPurchaseReturns);
            //SAPbobsCOM.Documents obj21 = oCompany.GetBusinessObjectFromXML(@"..\..\test\testexportdraft.txt", 0);
            //int ret = obj21.Add();

            //int ret = draft.SaveDraftToDocument();

            //SAPbobsCOM.Documents order = oCompany.GetBusinessObject(BoObjectTypes.oDrafts);
            //order.GetByKey(60);
            //order.DocDueDate = DateTime.Now;
            
            //order.SaveDraftToDocument();
            //order.GetByKey(5);
            //order.UpdateFromXML(@"..\..\test\add1.txt");
            //order.Browser.ReadXml(@"..\..\test\update1.txt", 0);
            //order.UpdateFromXML(@"..\..\test\update2.txt");
            //order.Update();
            //order.SaveToFile(@"..\..\test\update3.txt");
            //order.UpdateFromXML(@"..\..\test\update3.txt");
            //order.Lines.SetCurrentLine(0);
            //order.Lines.LineTotal = 1;
            //order.Update();
            //order.DocTotalFc = 900;
            //order.DiscountPercent = 8;
            //order.Confirmed = BoYesNoEnum.tYES;
            /*
            order.CardCode = "01000000016";
            order.DocDueDate = DateTime.Now;
            order.Lines.ItemCode = "batch";
            order.Lines.Quantity = 2;
            order.Lines.UnitPrice = 100;
            order.Lines.BatchNumbers.BatchNumber = "ba4";
            order.Lines.BatchNumbers.Quantity = 1;
            order.Lines.BatchNumbers.ManufacturerSerialNumber = "1";
            order.Lines.BatchNumbers.Add();
            order.Lines.BatchNumbers.BatchNumber = "ba5";
            order.Lines.BatchNumbers.Quantity = 1;
            order.Lines.BatchNumbers.ManufacturerSerialNumber = "1";
            order.Lines.BatchNumbers.Add();
            order.Lines.Add();
            order.Lines.ItemCode = "batch2";
            order.Lines.Quantity = 2;
            order.Lines.UnitPrice = 200;
            order.Lines.BatchNumbers.BatchNumber = "ba6";
            order.Lines.BatchNumbers.Quantity = 1;
            order.Lines.BatchNumbers.Add();
            order.Lines.BatchNumbers.BatchNumber = "ba7";
            order.Lines.BatchNumbers.Quantity = 1;
            order.Lines.BatchNumbers.Add();
            order.SaveToFile(@"..\..\test\batch2.txt");
            */ 
            //order.Add();

            SAPbobsCOM.Attachments2 att2 = oCompany.GetBusinessObject(BoObjectTypes.oAttachments2);
//             att2.Lines.SourcePath = "F:\\temp";
//             att2.Lines.FileName = "ObsLog0";
//             att2.Lines.FileExtension = "log";
//             att2.Lines.Add();
//             att2.Lines.SourcePath = "F:\\temp";
//             att2.Lines.FileName = "ObsLog1";
//             att2.Lines.FileExtension = "log";
//             att2.Add();

            att2.GetByKey(6);
            att2.Lines.Add();
            att2.Lines.SetCurrentLine(2);
            att2.Lines.SourcePath = "F:\\temp";
            att2.Lines.FileName = "output1";
            att2.Lines.FileExtension = "config";
            att2.Update();

            
            string errMsg = oCompany.GetLastErrorDescription();
        }
    }
}
